package com.tanhua.server.service;

import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    public void update(MultipartFile headPhoto,Integer  id) {

        // 1、上传图片
        String upload = ossTemplate.upload(headPhoto.getOriginalFilename());

        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(id));
        userInfo.setAvatar(upload);
        userInfoApi.update(userInfo);

    }

    // 根据id查询用户信息
    public UserInfoVo findById(Long userId) {

        UserInfo byId = userInfoApi.findById(userId);

        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(byId,vo);
        vo.setAge(byId.getAge().toString());


        return vo;

    }

    // 更新用户信息
    public void updateLL(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }
}